use rusqlite::{Connection, Result};
use std::collections::HashMap;

#[derive(Debug)]
struct Todo {
    id:i32,
    text: String,
    completed: bool,
}
fn vec1(){
    let mut v: Vec<i32> = Vec::new();
    let v2 = vec![1, 2, 3];
    v.push(5);
    v.push(6);
    v.push(7);
    v.push(8);
    
    let third: &i32 = &v[2];
    println!("The third element is {}", third);

    match v.get(2) {
        Some(third) => println!("The third element is {}", third),
        None => println!("There is no third element."),
    }
}
pub fn main(){
    // vec1();
    // post_todo(String::from("hello"),false);
    // put_todo(27,String::from("hello rust"),false);
    delete_todo(26);
    let all=get_todo();
    println!("{:?}",all);
}
fn main_db(){
   println!("runing");
   let r=main_do();
   match r {
        Ok(i) => {
            println!("ok");
        },
        Err(v) => {
            println!("{}",v);
        }
   };
}
pub fn delete_todo(id:i32){
    let conn = Connection::open("data.sqlite").expect("error open");
    conn.execute(
            "delete from  todos_todo where id=?",
            &[&id.to_string()]
        ).expect("error execute");
}
pub fn post_todo(text:String, completed:bool){
    let conn = Connection::open("data.sqlite").expect("error open");
    let  s_completed=if completed {String::from("1")} else{String::from("0")};
    conn.execute(
            "INSERT INTO todos_todo (text,completed) values (?1,?2)",
            &[&text.to_string(),&s_completed]
        ).expect("error execute");
}
pub fn put_todo(id:i32, text:String, completed:bool){
    let conn = Connection::open("data.sqlite").expect("error open");
    let  s_completed=if completed {String::from("1")} else{String::from("0")};
    conn.execute(
            "update todos_todo set text=?,completed=? where id=?",
            &[&text.to_string(),&s_completed,&id.to_string()]
        ).expect("error execute");
}

pub fn get_todo() -> Vec<(i32,String, bool)> {
    let mut vec=Vec::new();
    let conn = Connection::open("data.sqlite").expect("error open");
    let mut stmt = conn.prepare(
        "SELECT * from todos_todo",
    ).expect("error prepare");

    let todos=stmt.query_map([], |row| {
        Ok(Todo {
            id:row.get(0)?,
            text: row.get(1)?,
            completed: row.get(2)?,
        })
    }).expect("error query");
    for one in todos{
        match one {
            Ok(cat) => {
                vec.push((cat.id,cat.text,cat.completed));
            },
            Err(err) => {
                // println!("Failed to open the file.");
            }
        }
    }
    return vec;
}
pub fn main_do() -> Result<()> {

    let conn = Connection::open("data.sqlite")?;

    let mut todos = HashMap::new();
    todos.insert(String::from("Blue"), true);
    todos.insert(String::from("Black"), false);

    for (text, completed) in &todos {
        let  s_completed=if *completed {String::from("1")} else{String::from("0")};
        conn.execute(
            "INSERT INTO todos_todo (text,completed) values (?1,?2)",
            &[&text.to_string(),&s_completed]
        )?;
    }
    let mut stmt = conn.prepare(
        "SELECT * from todos_todo",
    )?;

    let todos = stmt.query_map([], |row| {
        Ok(Todo {
            id:row.get(0)?,
            text: row.get(1)?,
            completed: row.get(2)?,
        })
    })?;

    for cat in todos{
        println!("Found cat {:?}", cat);
    }

    Ok(())
}